package com.gz.purchase.library.listener;

import com.gz.purchase.library.pojo.MoveInStorageItemInfo;
import com.gz.purchase.library.util.Constants;
import com.gz.purchase.library.util.CrashHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 出库历史记录
 * author boolean . created at 2016/1/26 15:01
 */
public abstract class QueryOutStorageHistoryListener extends BaseListener {
    private String TAG = QueryOutStorageHistoryListener.class.getSimpleName();

    public QueryOutStorageHistoryListener(RequestCode requestCode) {
        super(requestCode);
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        try {
            int status = response.getInt(Constants.REQ_STATUS);
            String msg = response.getString(Constants.REQ_MSG);
            CrashHandler.i(TAG, "出库历史记录查询监听器msg=" + msg);
            CrashHandler.i(TAG, "出库历史记录查询监听器ret=" + status);
            CrashHandler.i(TAG,response.toString());
            switch (status) {
                case Constants.REQ_OK:
                    onResponseSuccess(MoveInStorageItemInfo.parse3(response.getJSONArray(Constants.REQ_RESULT)));
                    break;
                case Constants.REQ_QUERY_NO_DATA:
                    onNoSatisfedData();
                    break;
                default:
                    onResponseFailed();
            }
        } catch (JSONException e) {
            onFailure(statusCode, headers, e, new JSONObject());
        }
    }

    public abstract void onResponseSuccess(List<MoveInStorageItemInfo> list);

    public abstract void onNoSatisfedData();

    @Override
    public void onNoMoreData() {

    }

    @Override
    public void onOffline() {

    }

    @Override
    public void onFailure(int statusCode, Header[] headers,
                          Throwable throwable, JSONObject errorResponse) {
        CrashHandler.e(throwable);
        onResponseFailed();
    }
}
